#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import COOPGIS_Ajax, COOPGIS_FormGeography, COOPGIS_FormMisc,COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
import COOPGIS_FormBoardDistrict
import COOPGIS_FormLandmarkEditor
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from models import *
from django import forms
from django.db import transaction

@transaction.commit_on_success
def LandmarkPointEditor(request):   
    ApplicationName=djangoproject.settings.COOPGISLandmarkEditorApplicationName    
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/landmarkpointeditor.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))

    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Point', DefaultMap, SpecialBuildFormInfo={'LandmarkType':None, 'LandMark':None})
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    LandmarkTypeSelection, LandmarkSelection=COOPGIS_FormLandmarkEditor.ProcessLandmarkPointEditorForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Point', DefaultMap, SpecialBuildFormInfo={'LandmarkType':LandmarkTypeSelection, 'LandMark':LandmarkSelection})
    form=COOPGIS_FormLandmarkEditor.PopulateLandmarkPointEditorForm(form, LandmarkTypeSelection, LandmarkSelection)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None, Zoom=15)
    OpenLayersJavaScriptHeading=COOPGIS_Ajax.GetOpenLayersJavaScriptHeading()
    OpenLayersJavaScript=COOPGIS_Ajax.GetOpenLayersJavaScript(OpenLayersZoom, MapFileName, LayerName,
                            OpenLayersJavaScriptHeading, GeometryFeature = 'Point')
    t = loader.get_template('/opt/coopgis/djangoproject/templates/landmarkpointeditor.html')
    c = Context({'LoginStatus':LoginStatus, 'form':form,
                 'OpenLayersJavaScriptHeading': OpenLayersJavaScriptHeading,
                 'OpenLayersJavaScript': OpenLayersJavaScript})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def LandmarkLineEditor(request):
    ApplicationName=djangoproject.settings.COOPGISLandmarkEditorApplicationName  
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/landmarklineeditor.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))

    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Line', DefaultMap, SpecialBuildFormInfo={'LandmarkType':None, 'LandMark':None})
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    LandmarkTypeSelection, LandmarkSelection=COOPGIS_FormLandmarkEditor.ProcessLandmarkLineEditorForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Line', DefaultMap, SpecialBuildFormInfo={'LandmarkType':LandmarkTypeSelection, 'LandMark':LandmarkSelection})
    form=COOPGIS_FormLandmarkEditor.PopulateLandmarkLineEditorForm(form, LandmarkTypeSelection, LandmarkSelection)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None, Zoom=15)
    OpenLayersJavaScriptHeading=COOPGIS_Ajax.GetOpenLayersJavaScriptHeading()
    OpenLayersJavaScript=COOPGIS_Ajax.GetOpenLayersJavaScript(OpenLayersZoom, MapFileName, LayerName,
                            OpenLayersJavaScriptHeading, GeometryFeature = 'Line')
    t = loader.get_template('/opt/coopgis/djangoproject/templates/landmarklineeditor.html')
    c = Context({'LoginStatus':LoginStatus, 'form':form,
                 'OpenLayersJavaScriptHeading': OpenLayersJavaScriptHeading,
                 'OpenLayersJavaScript': OpenLayersJavaScript})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def LandmarkPolygonEditor(request):
    ApplicationName=djangoproject.settings.COOPGISLandmarkEditorApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/landmarkpolygoneditor.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Polygon', DefaultMap, SpecialBuildFormInfo={'LandmarkType':None, 'LandMark':None})
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    LandmarkTypeSelection, LandmarkSelection=COOPGIS_FormLandmarkEditor.ProcessLandmarkPolygonEditorForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'Polygon', DefaultMap, SpecialBuildFormInfo={'LandmarkType':LandmarkTypeSelection, 'LandMark':LandmarkSelection})
    form=COOPGIS_FormLandmarkEditor.PopulateLandmarkPolygonEditorForm(form, LandmarkTypeSelection, LandmarkSelection)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None, Zoom=15)
    OpenLayersJavaScriptHeading=COOPGIS_Ajax.GetOpenLayersJavaScriptHeading()
    OpenLayersJavaScript=COOPGIS_Ajax.GetOpenLayersJavaScript(OpenLayersZoom, MapFileName, LayerName,
                            OpenLayersJavaScriptHeading, GeometryFeature = 'Polygon')
    t = loader.get_template('/opt/coopgis/djangoproject/templates/landmarkpolygoneditor.html')
    c = Context({'LoginStatus':LoginStatus, 'form':form,
                 'OpenLayersJavaScriptHeading': OpenLayersJavaScriptHeading,
                 'OpenLayersJavaScript': OpenLayersJavaScript})
    return HttpResponse(t.render(c))

